package de.rj.urwalking.ui.fragments.dialog;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockDialogFragment;

import de.rj.urwalking.R;
import de.rj.urwalking.data.LoginResult;

/**
 * 
 * @author rjj
 * 
 */
public class LoginDialogFragment extends SherlockDialogFragment implements OnClickListener {

	public interface LoginDialogListener {
		void onFinishLoginDialog(String email, String password);
	}

	private EditText editEmail;
	private EditText editPassword;
	private Button okButton;
	private Button cancelButton;
	private boolean mPasswordEntered;
	private boolean mEmailIsValid;

	public LoginDialogFragment() {
		// Empty constructor required for DialogFragment
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_login_dialog, container);
		WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
	    wmlp.gravity = Gravity.LEFT;
		editEmail = (EditText) view.findViewById(R.id.editEmail);
		editEmail.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

				if (checkEmailAddress(s.toString())) {
					mEmailIsValid = true;
					updateOkButton();
				} else {
					mEmailIsValid = false;
					updateOkButton();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		editPassword = (EditText) view.findViewById(R.id.editPassword);
		editPassword.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

				if (s.toString().length() > 0) {
					mPasswordEntered = true;
					updateOkButton();
				} else {
					mPasswordEntered = false;
					updateOkButton();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		okButton = (Button) view.findViewById(R.id.okButton);
		okButton.setOnClickListener(this);
		okButton.setEnabled(false);
		cancelButton = (Button) view.findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(this);

		getDialog().setTitle(getString(R.string.login));

		LoginResult loginResult = null;
		if (getArguments() != null) {
			loginResult = (LoginResult) getArguments().getSerializable("loginResult");
		}

		if (loginResult != null) {
			editEmail.setText(loginResult.getEmail());
			if (!loginResult.isEmailOk()) {
				getDialog().setTitle(getString(R.string.error_email));
				editEmail.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.indicator_input_error, 0);
			} else if (!loginResult.isPasswordOk()) {
				getDialog().setTitle(getString(R.string.error_password));
				editPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.indicator_input_error, 0);
			}
		}

		// Show soft keyboard automatically
		editEmail.requestFocus();
		getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

		return view;

	}

	protected void updateOkButton() {

		if (mEmailIsValid && mPasswordEntered) {
			okButton.setEnabled(true);
		} else {
			okButton.setEnabled(false);
		}
	}

	protected boolean checkEmailAddress(String email) {

		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(email);
		boolean matchFound = m.matches();

		return matchFound;
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.okButton:
			// Return credentials to activity
			LoginDialogListener activity = (LoginDialogListener) getActivity();
			activity.onFinishLoginDialog(editEmail.getText().toString(), editPassword.getText().toString());
			this.dismiss();
			break;
		case R.id.cancelButton:
			this.dismiss();
			break;
		default:
			break;
		}

	}

}
